﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FtpCrawler
{
    public class FtpFileParserFactory
    {
        private static FtpFileParserFactory _factory = null;

        private FtpFileParserFactory(){}

        public static FtpFileParserFactory GetInstance()
        {
            if(_factory == null)
            {
                _factory = new FtpFileParserFactory();
            }
            return _factory;
        }

        /// <summary>
        /// 创建针对文件列表格式的文件解析器
        /// </summary>
        /// <param name="strStyle">文件列表格式</param>
        /// <returns>返回对应能解析该格式的解析器对象</returns>
        public IFtpFileParser CreateFileParser(string strStyle)
        {
            if(strStyle.Length > 10
                && Regex.IsMatch(strStyle.Substring(0, 10), "(-|d)((-|r)(-|w)(-|x)){3}"))
            {
                return new FtpFileParserInUnixStyle();
            } else if(strStyle.Length > 8
                && Regex.IsMatch(strStyle.Substring(0,8), "[0-9]{2}-[0-9]{2}-[0-9]{2}"))
            {
                return new FtpFileParserInWindowsStyle();
            } else
            {
                return null;
            }
        }
    }
}
